package daoImpls;

import java.sql.*;

import vo.Comment;

public class CommentDAOImpl<E> extends ObjectDAOImpl{

    public void add(Comment hero) {
       String sql ="insert into comment values (?,?,?,?);";
       try(Connection c =cp.getConnection(); PreparedStatement s = c.prepareStatement(sql)) {
            s.setString(1,hero.getCid());
            s.setString(2,hero.getUid());
            s.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
            s.setString(4,hero.getcMsg());
            s.execute();
            cp.returnConnection(c);
       } catch (SQLException e) {
           e.printStackTrace();
       }
    }


    public void update(Comment hero) {
        String sql ="update comment set cDate=?,cMsg=?  where CID=" + hero.getCid();
        try(Connection c =cp.getConnection(); PreparedStatement s = c.prepareStatement(sql)) {
            s.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
            s.setString(2,hero.getcMsg());
            System.out.println(sql);
            s.execute();
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void delete(String id) {
        super.delete(id,"cid");
    }
    
    public void deleteQuery(String ids) {
        super.delete(ids,"cid");
    }

    public Comment get(String id) {
        Comment comment = null;
        try(Connection c = cp.getConnection(); Statement s = c.createStatement()) {
            ResultSet rs = s.executeQuery("select * from comment where cid = " +id);
            cp.returnConnection(c);
            if (rs.next()){
                comment = new Comment();
                comment.setCid(id);
                comment.setUid(rs.getString(2));
                comment.setcDate(rs.getDate(3));
                comment.setcMsg(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comment;
    }
}
